package com.ys.utils.aspects;

import com.ys.base.EnumType;
import com.ys.utils.base.j2se.MapHandler;
import com.ys.utils.base.j2se.StringHandler;
import com.ys.utils.spring.config.SystemError;
import com.ys.utils.spring.properties.Config;
import com.ys.web.user.friends.mapper.GroupInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author: dhf
 * @ClassName: AccountContract
 * @Date: 2022/9/13 10:36
 * @Description:用户群验证
 * @Version: 1.0
 */

@Aspect
@Component
@Slf4j
public class UserGroupNumberCheckAspect {
    @Autowired
    private GroupInfoMapper groupInfoMapper;

    @Pointcut("@annotation(com.ys.utils.annotation.UserGroupNumberCheck)")
    private void anyMethod(){}//定义一个切入点,所有注解了Log的方法或类

    @Around("anyMethod()")
    public Object FreeOrderBlack(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取到请求参数
        Map<String, Object> fieldsName = MapHandler.getFieldsName(joinPoint);
        long uid = StringHandler.getLong(fieldsName.get("uid"),0);
        if (uid<=0) {
            SystemError.wrapBs(EnumType.ResponseCode.Fail.getCode(), "参数传递错误");
        }
        if(StringHandler.getInt(Config.pp.get("ys.user.group.number.check.opne"),0)==1) {
            log.info("UserGroupNumberCheckAspect---用户群验证--开起");
            if (uid != 0) {
                long count = groupInfoMapper.userGroupNumber(uid);
                if (count <= 0) {
                    SystemError.wrapBs(EnumType.ResponseCode.Fail.getCode(), "为保障资金安全，非招商推广群内成员没有该权限！");
                } else {
                    return joinPoint.proceed(joinPoint.getArgs());
                }
            }
        }else{
            log.info("UserGroupNumberCheckAspect---用户群验证--未开起");
            return joinPoint.proceed(joinPoint.getArgs());
        }
        return null;
    }
}
